package cheme.ProcessEditor.Policies;

import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.DirectEditPolicy;
import org.eclipse.gef.requests.DirectEditRequest;
import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.TextCellEditor;

import cheme.ProcessEditor.Parts.ITextModelPart;
import cheme.ProcessModel.Cmds.SetModelTextCmd;
import cheme.ProcessModel.Table.ITextModel;


public class PolicyWritableFieldDirectEdit extends DirectEditPolicy
{
	private String oldValue;

	/**
	 * @see DirectEditPolicy#getDirectEditCommand(org.eclipse.gef.requests.DirectEditRequest)
	 */
	protected Command getDirectEditCommand(DirectEditRequest request)
	{
		ITextModel model = (ITextModel) getHost().getModel();
		TextCellEditor cellEditor = (TextCellEditor)request.getCellEditor();
		
		return new SetModelTextCmd(model, (String)cellEditor.getValue());
	}

	/**
	 * @see DirectEditPolicy#showCurrentEditValue(org.eclipse.gef.requests.DirectEditRequest)
	 */
	protected void showCurrentEditValue(DirectEditRequest request)
	{
		String value = (String) request.getCellEditor().getValue();
		ITextModelPart part = (ITextModelPart)getHost();
		part.handleTextChange(value);
	}

	/**
	 * @param to Revert request
	 */
	protected void storeOldEditValue(DirectEditRequest request)
	{
		CellEditor cellEditor = request.getCellEditor();
		oldValue = (String) cellEditor.getValue();
	}

	/**
	 * @param request
	 */
	protected void revertOldEditValue(DirectEditRequest request)
	{
//		CellEditor cellEditor = request.getCellEditor();
//		cellEditor.setValue(oldValue);
		ITextModelPart part = (ITextModelPart)getHost();
		part.revertTextChange(oldValue);
	}
}
